package com.bite.ojcommonsecurity.service;

import com.bite.ojcommoncore.constants.CacheConstant;
import com.bite.ojcommoncore.constants.JwtConstants;
import com.bite.ojcommoncore.constants.RateConstants;
import com.bite.ojcommoncore.domain.entity.LoginSysUser;
import com.bite.ojcommoncore.utils.JwtUtils;
import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

public class TokenService {

    public static String createToken(Long userId , String userIdentify, String secret) {
        Map<String, Object> claims = new HashMap<>();
        // token负载需要保存redis中的key ， key = loginuser:id
        claims.put(JwtConstants.LOGIN_USER_ID ,userId); // 存储用户id
        claims.put(JwtConstants.LOGIN_USER_IDENTIFY, userIdentify); // 存储用户身份
        String token = JwtUtils.createToken(claims, secret);

        return token;
    }

    public static Claims  parseToken(String token, String secret) {
        Claims claims = JwtUtils.parseToken(token, secret);
        return claims;
    }

    public static String getTokenKey(String token,String secret) {
        Claims claims = JwtUtils.parseToken(token, secret);
        Long id = (Long) claims.get(JwtConstants.LOGIN_USER_ID);
        return CacheConstant.LOGIN_USER_CACHE_KEY + id;
    }

}
